var assert = require('assert');
var _ = require('lodash');

var ustr = require("../common/StringUtils");

describe('StringUtils', function(){
  describe('#hash()', function(){
    it('return hashed string', function(){
      assert.equal(32, ustr.hash('123456').length);
    })
  });
  describe('#randomChars(length)', function(){
    it('return random chars', function(){
      var len1 = _.random(0, 10);
      var len2 = _.random(100, 10000);
      assert.equal(len1, ustr.randomChars(len1).length);
      assert.equal(len2, ustr.randomChars(len2).length);
    })
  });
  describe('#n2id(id, length)', function(){
    it('return readable id with length', function(){
      assert.equal(4, ustr.n2id(10, 4).length); // 自动补位数
      assert.throws(
        function() {
          ustr.n2id(999999999, 2);
        },
        Error
      ); // 溢出异常
    })
  });
  describe('#id2n(id)', function(){
    it('return origin id', function(){
      var origin = _.random(9999);
      var id = ustr.n2id(origin, 4);
      assert.equal(origin, ustr.id2n(id));
      assert.equal(origin, ustr.id2n(ustr._ID_CHARS.charAt(0) + id));
    })
  });

});